﻿// -----------------------------------------------------------------------
// <copyright file="AbsOperationData.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using System;
using Sripirom.ATS.Core.ExceptionHandler;
using Sripirom.ATS.Core.Messaging;
using Sripirom.ATS.Model.ServiceLog;
namespace Sripirom.ATS.Services.OperationDispatchers
{

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public abstract class AbsOperationData<TChannelRequest, TChannelReply> 
        where TChannelReply : new()
    {
        protected readonly TChannelRequest _channelRequest;
        protected readonly TChannelReply _channelReply;
        protected ActivityStatus _activityStatus;
        public AbsOperationData(TChannelRequest channelRequest)
        {
            _channelRequest = channelRequest;

            _channelReply = new TChannelReply();
            var headerRequest = GetPropValue(_channelRequest, "Header");
            SetPropValue(_channelReply, "Header", headerRequest);

            _activityStatus = ActivityStatus.Begin;
        }

        public TChannelRequest ChannelRequest { get { return _channelRequest; } }

        public TChannelReply ChannelReply { get { return _channelReply; } }

        public TChannelReply GetChannelReply()
        {
            MessageOutput output = (MessageOutput)GetPropValue(_channelReply, "Output");
            if (String.IsNullOrEmpty(output.Result))
            {
                SetPropValue(output, "Result", "0");
                SetPropValue(output, "ErrorReason", "Success");
            }
            return _channelReply;
        }
        public TChannelReply CreateMessageException(Exception ex) 
        {
            MessageOutput messageOutput = new MessageOutput();
            if (ex != null && ex.GetType() == typeof(MessageException))
            {
                MessageException mEx = (MessageException)ex;
                messageOutput = MessageOutputFactory.Get().CreateOutputException(String.Empty, mEx.Code, 1, mEx.DictParameter);
            }
            else {
                messageOutput.Result = Errors.UNHANDLED_EXCEPTION; ;
                messageOutput.ErrorReason = "Unhandled exception";
            }                                                                       
            
            //var body = GetPropValue(_channelReply, "Body");
            var output = GetPropValue(_channelReply, "Output");
            SetPropValue(output, "Result", messageOutput.Result);
            SetPropValue(output, "ErrorReason", messageOutput.ErrorReason);

            return _channelReply;
        }


        #region Tools
    
        private void SetPropValue(object src, string propName, object value)
        {
            src.GetType().GetProperty(propName).SetValue(src, value, null);
        }
        private object GetPropValue(object src, string propName)
        {
            return src.GetType().GetProperty(propName).GetValue(src, null);
        }

        #endregion


    }
}
